package parkshop.pmcourse.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import parkshop.pmcourse.domain.Customer;
import parkshop.pmcourse.dto.User;
import parkshop.pmcourse.utils.ConstantUtil;


/**
 * Component 会让spring 自动扫描该类，这样该类则可以自动装配 spring拦截器需要实现该接口 HandlerInterceptor ，
 * 除此之外，spring 拦截器需要在 WebMvcConfigurerAdapter 中注册拦截器才能生效， 注册拦截器，参考conf 包下的
 * WebMvcConf.java
 */
@Component
public class UserSecurityInterceptor implements HandlerInterceptor {

    @Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		/*
		 * 不再使用 session 保存游客信息。
		 * 
		 * // 如果当前 session 密码为空，表明是访问的游客,需要登录 Info curUser=(Info)obj;
		 * if(curUser.getPassword()==null) {
		 * response.sendRedirect(request.getContextPath() + "/login"); return
		 * false; }
		 * 
		 * return true;
		 */
    	return true;
	}


    
    
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    	HttpSession session=request.getSession();
    	Object obj = session.getAttribute(ConstantUtil.nameOfCurUser);
        if (obj != null && (obj instanceof User)&&modelAndView!=null) {
            modelAndView.addObject(ConstantUtil.nameOfCurUser, (User)obj);
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
	